package com.citcc.api.netty;

import java.util.Date;
import java.util.List;

import org.jboss.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.citcc.api.netty.bean.ConsuMessage;
import com.citcc.api.netty.server.ChannelMap;
import com.citcc.api.netty.utils.BitUtil;
import com.citcc.api.netty.utils.HexUtil;
import com.citcc.api.netty.xc.xcserver.XcServerToClient;
import com.citcc.business.bean.CdzWarn;
import com.citcc.business.service.CdzWarnService;
import com.citcc.business.util.CollectionUtil;
import com.citcc.business.util.IdGenerator;
import com.citcc.framework.sms.JsonResult;

/**
 * @author liyachong
 * @date 2019/4/5
 * @time 15:58
 */
@RestController
@RequestMapping("/v3")
public class CommandStart {
	
	@Autowired
	private CdzWarnService cdzWarnService;
	
    private  Logger log = LoggerFactory.getLogger(CommandStart.class);
    /**
     * 
     * @param deviceNo
     * @param source
     * @param phone
     * @param time  单位秒
     * @param mode
     * @param money  单位分
     * @param dl     单位 0.01kw
     * @return
     */
    @PostMapping("/start")
    public JsonResult cmd(@RequestParam String terminalNo,@RequestParam String source,
    		@RequestParam String phone,long time,
    		@RequestParam String mode,
    		long money,long dl,@RequestParam String id) {
        
        ConsuMessage m = new ConsuMessage();
        m.setTerminalNo(terminalNo);
        m.setSource(Integer.parseInt(source));
        m.setPhone(phone);
        m.setMode(mode);//充电模式 0.自动充电 1.金额模式 2.时间模式 3电量模式
        m.setTime(time);//单位秒
        m.setMoney(money);
        m.setDl(dl);
        m.setId(id);
        int i = new XcServerToClient().startCmd(m);
       return new JsonResult(i,"成功");
    }
    @PostMapping("/stop")
    public JsonResult tcmd(@RequestParam String terminalNo,@RequestParam String source,@RequestParam String phone) {
    	log.info("终端号=="+terminalNo+"-->枪号="+source+"-->手机号:"+phone);
    	ConsuMessage m = new ConsuMessage();
    	m.setTerminalNo(terminalNo);
        m.setSource(Integer.parseInt(source));
        m.setPhone(phone);
        int i = new XcServerToClient().stopCmd(m);
        return new JsonResult(i,"成功");

    }
    
    public static void main(String[] args) {
		String s ="a8791b";
		String ss = new String(HexUtil.getBytesWithLengthAfter(4, s.getBytes()));
		System.out.println("s==="+ss);
	}
    
    @PostMapping("/test")
    public JsonResult test(@RequestParam byte v1,@RequestParam byte v2,@RequestParam byte v3,
    		@RequestParam byte v4,@RequestParam byte v5,@RequestParam byte v6,
    		@RequestParam byte v7,@RequestParam byte v8,@RequestParam long deviceNo,long qh) {
    	warnHandel(v1, v2, v3, v4, v5, v6, v7, v8, deviceNo, qh);
				return new JsonResult(200, "成功");
    	
    }
    
    public void warnHandel(byte v1,byte v2,byte v3,byte v4,byte v5,byte v6,byte v7,byte v8,long deviceNo,long qh){
 	   byte b1 = (byte) (v1 & 0xff);
 	   String s1 = BitUtil.byteToBit(b1);
 	   log.info("第1个字节接收到十进制值:"+v1+"-->二进制字符串:"+s1);
 	   String p1_1 = s1.substring(7);//触摸屏
 	   String s1_1 = "1-1";
 	   int v1_1 = Integer.parseInt(p1_1,2);
 	   insertAndUpdate(deviceNo,qh,s1_1,v1_1);
 	   
 	   
 	   String p1_2 = s1.substring(6,7);//读卡器
 	   String s1_2 = "1-2";
 	   int v1_2 = Integer.parseInt(p1_2,2);
 	   insertAndUpdate(deviceNo,qh,s1_2,v1_2);
 	   
 	   
 	   String p1_3_4 = s1.substring(4,6);//打印机
 	   String s1_3_4 = "1-3.4";
 	   int v1_3_4 = Integer.parseInt(p1_3_4,2);
 	   insertAndUpdate(deviceNo,qh,s1_3_4,v1_3_4);
 	   
 	   
 	   String p1_5 = s1.substring(3,4);//电能表
 	   String s1_5 = "1-5";
 	   int v1_5 = Integer.parseInt(p1_5,2);
 	   insertAndUpdate(deviceNo,qh,s1_5,v1_5);
 	   
 	   
 	   String p1_6_7_8 = s1.substring(0,3);//链接
 	   String s1_6_7_8 = "1-6.7.8";
 	   int v1_6_7_8 = Integer.parseInt(p1_6_7_8,2);
 	   insertAndUpdate(deviceNo,qh,s1_6_7_8,v1_6_7_8);
 	   
 	   
 	   
 	   byte b2 = (byte) (v2 & 0xff);
 	   String s2 = BitUtil.byteToBit(b2);
 	   log.info("第2个字节接收到十进制值:"+v2+"-->二进制字符串:"+s2);
 	   
 	   String p2_1_2 = s2.substring(6);//A相
 	   String s2_1_2 = "2-1.2";
 	   int v2_1_2 = Integer.parseInt(p2_1_2,2);
 	   insertAndUpdate(deviceNo,qh,s2_1_2,v2_1_2);
 	   
 	   
 	   String p2_3_4 = s2.substring(4,6);//b相
 	   String s2_3_4 = "2-3.4";
 	   int v2_3_4 = Integer.parseInt(p2_3_4,2);
 	   insertAndUpdate(deviceNo,qh,s2_3_4,v2_3_4);
 	   
 	   String p2_5_6 = s2.substring(2,4);//c相
 	   String s2_5_6 = "2-5.6";
 	   int v2_5_6 = Integer.parseInt(p2_5_6,2);
 	   insertAndUpdate(deviceNo,qh,s2_5_6,v2_5_6);
 	   
 	   String p2_7_8 = s2.substring(0,2);//输出过流
 	   String s2_7_8 = "2-7.8";
 	   int v2_7_8 = Integer.parseInt(p2_7_8,2);
 	   insertAndUpdate(deviceNo,qh,s2_7_8,v2_7_8);
 	   
 	   
 	   byte b3 = (byte) (v3 & 0xff);
 	   String s3 = BitUtil.byteToBit(b3);
 	   log.info("第3个字节接收到十进制值:"+v3+"-->二进制字符串:"+s3);
 	   
 	   String p3_1 = s3.substring(7);//急停
 	   String s3_1 = "3-1";
 	   int v3_1 = Integer.parseInt(p3_1,2);
 	   insertAndUpdate(deviceNo,qh,s3_1,v3_1);
 	   
 	   String p3_2 = s3.substring(6,7);//防雷器
 	   String s3_2 = "3-2";
 	   int v3_2 = Integer.parseInt(p3_2,2);
 	   insertAndUpdate(deviceNo,qh,s3_2,v3_2);
 	   
 	   String p3_3 = s3.substring(5,6);//电池连接状态
 	   String s3_3 = "3-3";
 	   int v3_3 = Integer.parseInt(p3_3,2);
 	   insertAndUpdate(deviceNo,qh,s3_3,v3_3);
 	   
 	   String p3_4 = s3.substring(4,5);//充电机监控通讯
 	   String s3_4 = "3-4";
 	   int v3_4 = Integer.parseInt(p3_4,2);
 	   insertAndUpdate(deviceNo,qh,s3_4,v3_4);
 	   
 	   String p3_5_6 = s3.substring(2,4);//直流母线绝缘
 	   String s3_5_6 = "3-5.6";
 	   int v3_5_6 = Integer.parseInt(p3_5_6,2);
 	   insertAndUpdate(deviceNo,qh,s3_5_6,v3_5_6);
 	   
 	   String p3_7_8 = s3.substring(0,2);//单体电池
 	   String s3_7_8 = "3-7.8";
 	   int v3_7_8 = Integer.parseInt(p3_7_8,2);
 	   insertAndUpdate(deviceNo,qh,s3_7_8,v3_7_8);
 	   
 	   byte b4 = (byte) (v4 & 0xff);
 	   String s4 = BitUtil.byteToBit(b4);
 	   log.info("第4个字节接收到十进制值:"+v4+"-->二进制字符串:"+s4);
 	   
 	   String p4_1 = s4.substring(7);//风扇
 	   String s4_1 = "4-1";
 	   int v4_1 = Integer.parseInt(p4_1,2);
 	   insertAndUpdate(deviceNo,qh,s4_1,v4_1);
 	   
 	   String p4_2 = s4.substring(6,7);//温度传感器
 	   String s4_2 = "4-2";
 	   int v4_2 = Integer.parseInt(p4_2,2);
 	   insertAndUpdate(deviceNo,qh,s4_2,v4_2);
 	   
 	   String p4_3 = s4.substring(5,6);//系统温度
 	   String s4_3 = "4-3";
 	   int v4_3 = Integer.parseInt(p4_3,2);
 	   insertAndUpdate(deviceNo,qh,s4_3,v4_3);
 	   
 	   String p4_4 = s4.substring(4,5);//BMS通讯
 	   String s4_4 = "4-4";
 	   int v4_4 = Integer.parseInt(p4_4,2);
 	   insertAndUpdate(deviceNo,qh,s4_4,v4_4);
 	   
 	   byte b5 = (byte) (v5 & 0xff);
 	   String s5 = BitUtil.byteToBit(b5);
 	   log.info("第5个字节接收到十进制值:"+v5+"-->二进制字符串:"+s5);
 	   
 	   String p5_1 = s5.substring(7);//交流输入欠压
 	   String s5_1 = "5-1";
 	   int v5_1 = Integer.parseInt(p5_1,2);
 	   insertAndUpdate(deviceNo,qh,s5_1,v5_1);
 	   
 	   String p5_2 = s5.substring(6,7);//交流输入过压
 	   String s5_2 = "5-2";
 	   int v5_2 = Integer.parseInt(p5_2,2);
 	   insertAndUpdate(deviceNo,qh,s5_2,v5_2);
 	   
 	   String p5_3 = s5.substring(5,6);//输出过压
 	   String s5_3 = "5-3";
 	   int v5_3 = Integer.parseInt(p5_3,2);
 	   insertAndUpdate(deviceNo,qh,s5_3,v5_3);
 	   
 	   String p5_4 = s5.substring(4,5);//充电模块监控响应超时
 	   String s5_4 = "5-4";
 	   int v5_4 = Integer.parseInt(p5_4,2);
 	   insertAndUpdate(deviceNo,qh,s5_4,v5_4);
 	   
 	   String p5_5 = s5.substring(3,4);//模块通信中断
 	   String s5_5 = "5-5";
 	   int v5_5 = Integer.parseInt(p5_5,2);
 	   insertAndUpdate(deviceNo,qh,s5_5,v5_5);
 	   
 	   String p5_6 = s5.substring(2,3);//交流输入欠压
 	   String s5_6 = "5-6";
 	   int v5_6 = Integer.parseInt(p5_6,2);
 	   insertAndUpdate(deviceNo,qh,s5_6,v5_6);
 	   
 	   String p5_7 = s5.substring(1,2);//模块故障
 	   String s5_7 = "5-7";
 	   int v5_7 = Integer.parseInt(p5_7,2);
 	   insertAndUpdate(deviceNo,qh,s5_7,v5_7);
 	   
 	   String p5_8 = s5.substring(0,1);//模块保护
 	   String s5_8 = "5-8";
 	   int v5_8 = Integer.parseInt(p5_8,2);
 	   insertAndUpdate(deviceNo,qh,s5_8,v5_8);
 	   
 	   byte b6 = (byte) (v6 & 0xff);
 	   String s6 = BitUtil.byteToBit(b6);
 	   log.info("第6个字节接收到十进制值:"+v6+"-->二进制字符串:"+s6);
 	   
 	   String p6_1 = s6.substring(7);//充电枪故障
 	   String s6_1 = "6-1";
 	   int v6_1 = Integer.parseInt(p6_1,2);
 	   insertAndUpdate(deviceNo,qh,s6_1,v6_1);
 	   
 	   String p6_2 = s6.substring(6,7);//握手超时
 	   String s6_2 = "6-2";
 	   int v6_2 = Integer.parseInt(p6_2,2);
 	   insertAndUpdate(deviceNo,qh,s6_2,v6_2);
 	   
 	   String p6_3 = s6.substring(5,6);//配置超时
 	   String s6_3 = "6-3";
 	   int v6_3 = Integer.parseInt(p6_3,2);
 	   insertAndUpdate(deviceNo,qh,s6_3,v6_3);
 	   
 	   String p6_4 = s6.substring(4,5);//接收充电请求指令超时
 	   String s6_4 = "6-4";
 	   int v6_4 = Integer.parseInt(p6_4,2);
 	   insertAndUpdate(deviceNo,qh,s6_4,v6_4);
 	   
 	   String p6_5 = s6.substring(3,4);//模块交流缺相告警
 	   String s6_5 = "6-5";
 	   int v6_5 = Integer.parseInt(p6_5,2);
 	   insertAndUpdate(deviceNo,qh,s6_5,v6_5);
 	   
 	   String p6_6 = s6.substring(2,3);//模块严重不均流
 	   String s6_6 = "6-6";
 	   int v6_6 = Integer.parseInt(p6_6,2);
 	   insertAndUpdate(deviceNo,qh,s6_6,v6_6);
 	   
 	   String p6_7 = s6.substring(1,2);//门禁开关告警
 	   String s6_7 = "6-7";
 	   int v6_7 = Integer.parseInt(p6_7,2);
 	   insertAndUpdate(deviceNo,qh,s6_7,v6_7);
 	   
 	   String p6_8 = s6.substring(0,1);//电池异常
 	   String s6_8 = "6-8";
 	   int v6_8 = Integer.parseInt(p6_8,2);
 	   insertAndUpdate(deviceNo,qh,s6_8,v6_8);
 	   
 	   byte b7 = (byte) (v7 & 0xff);
 	   String s7 = BitUtil.byteToBit(b7);
 	   log.info("第7个字节接收到十进制值:"+v7+"-->二进制字符串:"+s7);
 	   String p7_1 = s7.substring(7);//系统倾倒或严重振动
 	   String s7_1 = "7-1";
 	   int v7_1 = Integer.parseInt(p7_1,2);
 	   insertAndUpdate(deviceNo,qh,s7_1,v7_1);
 	   
 	   String p7_2 = s7.substring(6,7);//充电枪连接异常
 	   String s7_2 = "7-2";
 	   int v7_2 = Integer.parseInt(p7_2,2);
 	   insertAndUpdate(deviceNo,qh,s7_2,v7_2);
 	   
 	   String p7_3 = s7.substring(5,6);//放电电阻异常
 	   String s7_3 = "7-3";
 	   int v7_3 = Integer.parseInt(p7_3,2);
 	   insertAndUpdate(deviceNo,qh,s7_3,v7_3);
 	   
 	   String p7_4 = s7.substring(4,5);//枪头温度过高
 	   String s7_4 = "7-4";
 	   int v7_4 = Integer.parseInt(p7_4,2);
 	   insertAndUpdate(deviceNo,qh,s7_4,v7_4);
 	   
 	   String p7_5 = s7.substring(3,4);//K1接触器异常
 	   String s7_5 = "7-5";
 	   int v7_5 = Integer.parseInt(p7_5,2);
 	   insertAndUpdate(deviceNo,qh,s7_5,v7_5);
 	   
 	   String p7_6 = s7.substring(2,3);//系统输出过流
 	   String s7_6 = "7-6";
 	   int v7_6 = Integer.parseInt(p7_6,2);
 	   insertAndUpdate(deviceNo,qh,s7_6,v7_6);
 	   
 	   String p7_7 = s7.substring(1,2);//U3-U3通讯异常
 	   String s7_7 = "7-7";
 	   int v7_7 = Integer.parseInt(p7_7,2);
 	   insertAndUpdate(deviceNo,qh,s7_7,v7_7);
 	   
 	   String p7_8 = s7.substring(0,1);//电子锁异常
 	   String s7_8 = "7-8";
 	   int v7_8 = Integer.parseInt(p7_8,2);
 	   insertAndUpdate(deviceNo,qh,s7_8,v7_8);
 	   
    }
    
    public void insertAndUpdate(long deviceNo,long qh,String warnSpot,int warnDesc){
 	   if(warnDesc == 0){//该告警点正常
 		   CdzWarn warn = new CdzWarn();
 		   warn.setStakeCode(deviceNo+"");
 		   warn.setGunCode(qh+"");
 		   warn.setWarnSpot(warnSpot);
 		   warn.setStartTime(new Date());
 		   List<CdzWarn> list = cdzWarnService.selectCdzWarn(warn);
 		   if(CollectionUtil.isNotEmpty(list)){
 			   Date endTime = list.get(0).getEndTime();
 			   Date startTime = list.get(0).getStartTime();
 			   if(endTime==null){//没有结束时间，需要更新
 				 //存在一条这样的告警信息
 				   String id = list.get(0).getId();
 				   CdzWarn w = new CdzWarn();
 				   w.setId(id);
 				   endTime = new Date();
 				   long time = (endTime.getTime()-startTime.getTime())/1000;
 				   w.setUpdateTime(endTime);
 				   w.setEndTime(endTime);
 				   w.setWarnTime(time+"");
 				   w.setIsEffect("1");
 				   cdzWarnService.updateByPrimaryKey(w);
 			   }
 		   }
 	   }else{
 		   CdzWarn warn = new CdzWarn();
 		   warn.setStakeCode(deviceNo+"");
 		   warn.setGunCode(qh+"");
 		   warn.setWarnSpot(warnSpot);
 		   warn.setWarnDesc(warnDesc+"");
 		   List<CdzWarn> list = cdzWarnService.selectCdzWarn(warn);
 		   if(CollectionUtil.isNotEmpty(list)){
 			   Date endTime = list.get(0).getEndTime();
 			   if(endTime != null){//查询出的记录是已恢复告警的，本次需要再新增一条告警记录
 				   CdzWarn cdzWarn = new CdzWarn();
 				   cdzWarn.setId(IdGenerator.generateId());
 				   cdzWarn.setStakeCode(deviceNo+"");
 				   cdzWarn.setGunCode(qh+"");
 				   cdzWarn.setWarnSpot(warnSpot);
 				   cdzWarn.setWarnDesc(warnDesc+"");
 				   cdzWarn.setStartTime(new Date());
 				   cdzWarn.setCreateTime(new Date());
 				   cdzWarnService.insert(cdzWarn);
 			   }
 		   }else{
 			   //数据为空
 			   if(warnDesc!=0){//非正常插入一条记录
 				   CdzWarn cdzWarn = new CdzWarn();
 				   cdzWarn.setId(IdGenerator.generateId());
 				   cdzWarn.setStakeCode(deviceNo+"");
 				   cdzWarn.setGunCode(qh+"");
 				   cdzWarn.setWarnSpot(warnSpot);
 				   cdzWarn.setWarnDesc(warnDesc+"");
 				   cdzWarn.setStartTime(new Date());
 				   cdzWarn.setCreateTime(new Date());
 				   cdzWarnService.insert(cdzWarn);
 			   }
 		   }
 	   }
 	   
    }
    
    @PostMapping("/isOpen")
    public JsonResult isopen(@RequestParam String  deviceNo){
    	log.info("桩号----------:"+deviceNo);
    	if(StringUtils.isEmpty(deviceNo)){
    		return new JsonResult(-1, "失败");
    	}
    	Channel channel = ChannelMap.getChannelByName(deviceNo);
    	if(channel == null){
    		return new JsonResult(-1, "失败");
    	}
    	boolean con = channel.isConnected();
    	boolean op = channel.isOpen();
    	if(con && op){
    		return new JsonResult(200, "成功");
    	}
    	log.info("桩号----------:"+deviceNo+"-->链接状态:"+con+"-->开启状态:"+op);
		return new JsonResult(-1, "失败");
    	
    }


}
